"""
@Author：WangYuXiang
@E-mile：Hill@3io.cc
@CreateTime：2021/4/27 10:01
@DependencyLibrary：无
@MainFunction：无
@FileDoc： 
    decorator.py
    文件说明
@ChangeHistory:
    datetime action why
    example:
    2021/4/27 10:01 change 'Fix bug'
        
"""


def lazy_property(func):
    """
    懒加载装饰器
    example:
        class A():
            @lazy_property
            def user():
                ...复杂运算...
                return res
    :param func: 需要加载的对象
    :return:
    """
    attr_name = "_lazy_" + func.__name__

    @property
    def _lazy_property(self):
        if not hasattr(self, attr_name):
            setattr(self, attr_name, func(self))
        return getattr(self, attr_name)

    return _lazy_property
